package com.pig4cloud.pig.module.controller.setMeal;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.setMeal.SetMeal;
import com.pig4cloud.pig.module.api.entity.setMeal.SetMealInfo;
import com.pig4cloud.pig.module.service.setMeal.ISetMealInfoService;
import com.pig4cloud.pig.module.service.setMeal.ISetMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 套餐详情Controller
 *
 * @author ruoyi
 * @date 2022-06-06
 */
@Validated
@Api(value = "套餐详情控制器", tags = {"套餐详情管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/meal/info")
public class SetMealInfoController {

    private final ISetMealInfoService setMealInfoService;
	private final ISetMealService setMealService;

//	@Value("${ruoyi.imgUrl}")
//	private String imgUrl;

	@Value("${ruoyi.imgUrlOss}")
	private String imgUrlOss;

	@Value("${ruoyi.profile}")
	private String uploadPath;
    /**
     * 查询套餐详情列表
     */
    @ApiOperation("查询套餐详情列表")
    @PreAuthorize("@pms.hasPermission('module:mealInfo:list')")
    @GetMapping("/list")
    public TableDataInfo<SetMealInfo> list(@Validated SetMealInfo setMealInfo) {
        QueryWrapper<SetMealInfo> qw = new QueryWrapper<>();
        if(setMealInfo.getMealId()!=null){
        	qw.eq("meal_id",setMealInfo.getMealId());
		}

        TableDataInfo<SetMealInfo> td = PageUtils.buildDataInfo(setMealInfoService.page(PageUtils.buildPage(),qw));
        List<SetMealInfo> list = td.getData();
        if(CollUtil.isNotEmpty(list)){
        	for (SetMealInfo info : list){
				if(StrUtil.isNotEmpty(info.getInfoImg())){
					info.setInfoImgPath(imgUrlOss + info.getInfoImg());
				}
			}
		}
        return td;
    }

    /**
     * 获取套餐详情详细信息
     */
    @ApiOperation("获取套餐详情详细信息")
    @PreAuthorize("@pms.hasPermission('module:mealInfo:query')")
    @GetMapping("/{id}")
    public R<SetMealInfo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        SetMealInfo setMealInfo = setMealInfoService.getById(id);
		SetMeal setMeal = setMealService.findById(setMealInfo.getMealId());
		if(setMeal!=null){
			setMealInfo.setMealName(setMeal.getMealName());
			if(StrUtil.isNotEmpty(setMealInfo.getInfoImg())){
				setMealInfo.setInfoImgPath(imgUrlOss + setMealInfo.getInfoImg());
			}
		}
        return R.ok(setMealInfo);
    }

    /**
     * 新增套餐详情
     */
    @ApiOperation("新增套餐详情")
    @PreAuthorize("@pms.hasPermission('module:mealInfo:add')")
    @SysLog("套餐详情")
    @PostMapping()
    public R<Void> add(@Valid @RequestBody SetMealInfo setMealInfo) {
        return R.okByRow(setMealInfoService.save(setMealInfo) ? 1 : 0);
    }

    /**
     * 修改套餐详情
     */
    @ApiOperation("修改套餐详情")
    @PreAuthorize("@pms.hasPermission('module:mealInfo:edit')")
    @SysLog("套餐详情")
    @PutMapping()
    public R<Void> edit(@Valid @RequestBody SetMealInfo setMealInfo) {
        return R.okByRow(setMealInfoService.updateById(setMealInfo) ? 1 : 0);
    }

    /**
     * 删除套餐详情
     */
    @ApiOperation("删除套餐详情")
    @PreAuthorize("@pms.hasPermission('module:mealInfo:remove')")
    @SysLog("套餐详情")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(setMealInfoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
